﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using QuanLyDuocPham.Controllers.QuanLyHeThong.DanhMuc;
using QuanLyDuocPham.Controllers.QuanLyHeThong.NhapXuat;



namespace QuanLyDuocPham.View.QuanLyHeThong.NhapXuat
{
    /// <summary>
    /// Interaction logic for NHAPHANG_Add_View.xaml
    /// </summary>
    public partial class NHAPHANG_Add_View : Window
    {
        public NHAPHANG_Add_View()
        {
            InitializeComponent();
        }
        public delegate void added();
        public added done;
        NHANVIEN_Control nvctrl = new NHANVIEN_Control();
        NCC_Control nccctrl = new NCC_Control();
        NHAPHANG_Control nhctrl = new NHAPHANG_Control();
        NHAP_CHITIET_Control nhctctrl = new NHAP_CHITIET_Control();
        KHO_Control khoctrl = new KHO_Control();
        DUOCPHAM_Control dpctrl = new DUOCPHAM_Control();
        string ma_nhap;
        public string _ma_nhap
        {
            set
            {
                ma_nhap = value;
            }
        }
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            ma_nhap = nhctrl.loadID_moi();
            nvctrl.loadComboBox(cmbNhanVien);
            nccctrl.loadComboBox(cmbNCC);
            khoctrl.loadComboBox(cmbKho);
            btnCloseNHAPCT.IsEnabled = false;
        }
        UIElement ui;

        private void btnok_Click_1(object sender, RoutedEventArgs e)
        {
            btnok.IsEnabled = false;
            nhctrl.addData(cmbKho, ma_nhap, cmbNhanVien, cmbNCC, txtMa_Lo, dtpNgayNhap, chbCo_HD, txtTLCK, dtpHanTT, txtDaTT);
            if (done != null)
            {
                done();
            }
            hideNHAPHANG();
            Controllers.UC.QuanLyHeThong.NhapXuat.UC_NHAP_CHITIET ul = new Controllers.UC.QuanLyHeThong.NhapXuat.UC_NHAP_CHITIET();
            ul._ma_nhap = ma_nhap;
            ul._is_edit = false;
            var cmbKhoSelect = (KHO_Get_ListResult)cmbKho.SelectedItem;
            
            ul.Margin = new Thickness(0, 0, 0, 0);
            MainFrame.Children.Add(ul);
            ui = ul;
        }
        private void hideNHAPHANG()
        {

            cmbNhanVien.IsEnabled = false;
            cmbNCC.IsEnabled = false;
            cmbKho.IsEnabled = false;
            dtpNgayNhap.IsEnabled = false;
            chbCo_HD.IsEnabled = false;
            txtTLCK.IsEnabled = false;
            txtDienGiai.IsEnabled = false;
            dtpHanTT.IsEnabled = false;
            txtDaTT.IsEnabled = false;
            chbDa_Xoa.IsEnabled = false;
        }
        private void clearNHAPHANG()
        {
            ma_nhap = nhctrl.loadID_moi();

            cmbNhanVien.SelectedItem = null;
            cmbNCC.SelectedItem = null;
            cmbKho.SelectedItem = null;
            dtpNgayNhap.SelectedDate = DateTime.Now;
            chbCo_HD.IsChecked = false;
            txtTLCK.Clear();
            txtDienGiai.Clear();
            dtpHanTT.SelectedDate = DateTime.Now;
            txtDaTT.Clear();
            chbDa_Xoa.IsChecked = false;
        }
        private void btnClose_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void btnClearNHAPHANG_Click(object sender, RoutedEventArgs e)
        {
            clearNHAPHANG();
        }

        private void btnCloseNHAPCT_Click(object sender, RoutedEventArgs e)
        {
            clearNHAPHANG();
            MainFrame.Children.Remove(ui);
            nhctctrl.clearTemp2();
            btnok.IsEnabled = true;
        }

        private void txtDaTT_TextChanged(object sender, TextChangedEventArgs e)
        {
            var cmbKhoSelect = (KHO_Get_ListResult)cmbKho.SelectedItem;
            if (nhctctrl.TongTien(ma_nhap,cmbKhoSelect.MA_KHO) == int.Parse(txtDaTT.Text))
            {
                chbDaTToan.IsChecked = true;
            }
            else chbDaTToan.IsChecked = false;
        }

    }
}
